#!/bin/bash
set -e
keyStore=$1
domain=$2
trustStore=$3
trustStoreDir=`dirname $trustStore`

if [  -f "$keyStore" ]; then
	rm $keyStore
fi;

if [ $(keytool -list   -keystore $trustStore -storepass 123456 | grep -c $domain) -gt 0 ]; then
	keytool -delete -alias $domain -keystore $trustStore  -keypass 123456 -storepass 123456
fi;


keytool -genkey  \
-alias $domain  \
-keypass 123456  \
-keyalg RSA  \
-keysize 1024  \
-validity 36500  \
-keystore  $keyStore \
-storepass 123456  \
-dname "CN=$domain,OU=上海理想, O=工业制造部, L=上海, ST='上海', C='中国'"

cd $trustStoreDir
keytool -export -alias $domain -storepass 123456 -file $domain.cer -keystore $keyStore


/usr/bin/expect<<EOF
spawn keytool -import -alias $domain -file $trustStoreDir/$domain.cer -keystore $trustStore -trustcacerts -storepass 123456  -keypass 123456
expect "]:"
send "y\r"
interact
EOF

